<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px
        }

        .box2 {
            width: 100px;
            height: 100px;
            background: blue;
            position: absolute;
            left: 300px;
            top: 0px
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <script>
        // 红色方块拖动
        var box1Ele = document.querySelector(".box1");
        var box2Ele = document.querySelector(".box2");

        box1Ele.onmousedown = function (e) {
            var x = e.clientX - this.offsetLeft;
            var y = e.clientY - this.offsetTop;
            this.onmousemove = function (e) {
                var xx = e.clientX;
                var yy = e.clientY;
                this.style.left = xx - x + "px";
                this.style.top = yy - y + "px";
                var AminX = box1Ele.offsetLeft;
                var AmaxX = box1Ele.offsetLeft + box1Ele.offsetWidth;
                var BminX = box2Ele.offsetLeft;
                var BmaxX = box2Ele.offsetLeft + box2Ele.offsetWidth;


                var AminY = box1Ele.offsetTop;
                var AmaxY = box1Ele.offsetTop + box1Ele.offsetHeight;
                var BminY = box2Ele.offsetTop;
                var BmaxY = box2Ele.offsetTop + box2Ele.offsetHeight;



                if(AmaxX>BminX && AminX<BmaxX  && AmaxY>BminY && AminY<BmaxY){
                    console.log("x轴上碰撞了");
                }

               

            }

            // getImageData

            document.onmouseup = function () {
                box1Ele.onmousemove = "";
            }
        }


    </script>
</body>

</html>